<?php
//header('Content-Type: text/html; charset=EUC-JP');
if(!defined('INC_PATH')){
	define("INC_PATH","../../../");
}
require_once(INC_PATH."lib_new/db.php");
$num = $_REQUEST["num"];
$type = $_REQUEST["type"];
$member_id = $_COOKIE['sav_memberid'];

if ($member_id != '' && is_numeric($member_id))
	{
		$con = con_open();
		//トランザクション-スタート用
		$sql_transaction_begin = "BEGIN;";
		//トランザクション-コミット用
		$sql_transaction_commit = "COMMIT;";
		//トランザクション-戻る用
		$sql_transaction_roolback = "ROLLBACK;";

		pg_query($con, $sql_transaction_begin);
		$sql_check = "
		SELECT fukubiki FROM member
		WHERE memberid = $member_id ";
		$res_check = pg_query($con, $sql_check);
		if(pg_fetch_result($res_check, "fukubiki") < $num){
			echo 'Var.set(error,宝くじ交換券が足りません。);';
			con_close($con);
			exit;
		}
		
		$sql_up = "UPDATE member SET fukubiki = (fukubiki - ".$num.")
		WHERE memberid = $member_id 
		AND fukubiki >= ".$num;
		if($type == 1){
			$sql_in = "INSERT INTO smd_takarakuji_member (
			member_id
			,type
			,num
			) VALUES (
			$member_id
			,$type
			,$num
			) ";
		}else{
			$sql_in = "INSERT INTO smd_takarakuji_member (
			member_id
			,type
			,num
			) VALUES ";
			$sql_add="";
			for($i = 0;$i<$num;$i += 10){
				$sql_in .= $sql_add." ($member_id,$type,10) ";
				$sql_add = ",";
			}
		}
		$res = pg_query($con, $sql_up);
		$ret = pg_query($con, $sql_in);
		$update_num = 0;
		if($res){
			$update_num = pg_affected_rows($res);
		}
		if ($update_num>0 && $ret)
		{
			$file_log = fopen("./log/getticket_ok_log.txt","a");
			fwrite($file_log,date ("Y-m-d H:i:s",time())."\n".$member_id."\n".$sql_up."\n".$res."\n".$sql_in."\n".$ret."\n");
			fclose($file_log);
			pg_query($con, $sql_transaction_commit);
			$_SESSION["login"]["fukubiki"] = pg_fetch_result($res_check, "fukubiki") - $num;
		}else{
			$file_log = fopen("./log/getticket_error_log.txt","a");
			fwrite($file_log,date ("Y-m-d H:i:s",time())."\n".$member_id."\n".$sql_up."\n".$sql_in."\n");
			fclose($file_log);
			pg_query($con, $sql_transaction_roolback);
			echo 'Var.set(error,ｴﾗｰが発生しました。);';
			con_close($con);
			exit;
		}

	con_close($con);
	}
//$regist_date = date ("Y-m-d H:i:s",time());
$year = date ("Y",time()) - 1988;
$month = date ("m",time());
$day = date ("d",time());
echo '
Var.set(buyNum,'.$num.');
Var.set(startdate,平成'.$year.'年'.$month.'月'.$day.'日);
Var.set(enddate,平成'.$year.'年'.$month.'月25日);
';
/**
echo '<data>
	<setVariable type="text" name="buyNum" value="'.$num.'" />
	<setVariable type="text" name="year" value="2011" />
	<setVariable type="text" name="month" value="01" />
	<setVariable type="text" name="team" value="A" />
	<setVariable type="text" name="getnum" value="001225" />
	<setVariable type="text" name="adurl" value="./images/item/11254.gif" />	
	<setVariable type="text" name="startdate" value="平成'.$year.'年'.$month.'月'.$day.'日" />
	<setVariable type="text" name="enddate" value="平成'.$year.'年'.$month.'月25日" />
	<setVariable type="text" name="error" value="null" />
	<phpover />
</data>';*/
?>